import { configureStore, combineReducers } from "@reduxjs/toolkit";
import { persistStore, persistReducer } from "redux-persist";
// import sessionStorage from "redux-persist/lib/storage/session";
import storage from "redux-persist/lib/storage";

import logger from "redux-logger";
import reduxThunk from "redux-thunk";
import auth from "@/store/modules/auth";
import global from "@/store/modules/global";

//create reducer
const reducer = combineReducers({
	auth,
	global
});

// 持久化配置
const persistConfig = {
	key: "redux-state",
	storage: storage,
	whitelist: ["global", "auth"]
};
const persistReducerConfig = persistReducer(persistConfig, reducer);

// redux middleWares
const middleWares = [reduxThunk, logger];

// store
export const store = configureStore({
	reducer: persistReducerConfig,
	middleware: middleWares,
	devTools: true
});

// 持久化的store
export const persistor = persistStore(store);

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
